//
//  MeatShopListViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/19.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatShopListViewController.h"
#import "MeatComplaintViewConroller.h"
#import "MeatShopViewController.h"
#import "MeatShopTransferViewController.h"

#import "MeatShopHeader.h"

@interface MeatShopListViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *controls;
@property (nonatomic, strong) MeatShopHeader *header;
@property (nonatomic, strong) UIImageView *showIcon;


@end

@implementation MeatShopListViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.contentScrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
}

#pragma mark - init method

- (void)setUpView {
    [self setupNavBar];
    [self setupChildViewControllers];
    [self setupChildSetting];
    
}

- (void)setupNavBar{
    [self.view addSubview:self.header];
}

- (void)setupChildViewControllers{
    
    kWeakSelf(self);
    [self.controls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIViewController *vc = (UIViewController *)obj;
        vc.title = weakself.titles[idx];
        [weakself addChildViewController:vc];
    }];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
//    kWeakSelf(self);
//    [self setUpContentViewFrame:^(UIView *contentView) {
//        CGFloat contentX = 0;
//        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight;
//        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
//    }];
}

- (void)setupChildSetting
{
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        //CGFloat contentH = KScreenHeight - TJ_MeatShop_Header - TJ_MeatShop_Bottomer  ;
        
        CGFloat contentH = KScreenHeight   ;

        contentView.frame = CGRectMake(contentX, TJ_MeatShop_Header, KScreenWidth, contentH);
    }];
    
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight,CGFloat *titleWidth) {
        
        // 设置标题字体
        *titleFont = mMediumFont(16);
        *norColor = UIColorFromRGB(0x333333);
        *selColor = mMainColor;
        *titleHeight = 42;
        *titleScrollViewColor = KWhiteColor;
    }];
    
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = mMainColor;
        *isUnderLineEqualTitleWidth = YES;
        *underLineH = 3;
    }];
    
}

#pragma mark - setter and getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"售卖中的猪",@"转让中的猪"];
    }
    return _titles;
}

- (NSArray *)controls
{
    if (!_controls) {
        
        MeatShopViewController *oneVC = [[MeatShopViewController alloc] init];
        MeatShopTransferViewController *twoVC = [[MeatShopTransferViewController alloc]init];
        _controls = @[oneVC,twoVC];
    }
    return _controls;
}


- (MeatShopHeader *)header{
    
    if (!_header) {
        _header = [[MeatShopHeader alloc] init];
        
        UIWindow *window = [[UIApplication sharedApplication] keyWindow];
        [window addSubview:self.showIcon];
       // [_header addSubview:self.showIcon];
        
        //________
        @weakify(self);
        _header.leftNavBlock = ^{
            @strongify(self);
            [self.navigationController popViewControllerAnimated:YES];
        };
        
        
        //_________
        _header.rightListBlock = ^(NSInteger params) {
            @strongify(self);
            if (params == 2) {
                
                if (self.showIcon.alpha == 0 ) {
                    
                    [UIView animateWithDuration:AnimationTime animations:^{
                        self.showIcon.alpha = 1;
                    }];
                    
                }else{
                    [self hideShopIcon];
                }
            }else{
                
                if (self.showIcon.alpha == 1) {
                    [self hideShopIcon];
                }
            }
        };
        
    }
    return _header;
}


- (UIImageView *)showIcon{
    
    if (!_showIcon) {
        _showIcon = [UIImageView makeImageByImageName:@"meat_shop_box_bg" cornerRadius:0];
        _showIcon.backgroundColor = KClearColor;
        _showIcon.size = CGSizeMake(144, 154);
        _showIcon.mj_x = KScreenWidth - _showIcon.width - 15;
        _showIcon.mj_y = TJ_StatusBarHeight + 40;
        _showIcon.alpha = 0;
        _showIcon.userInteractionEnabled = YES;
        
        NSArray *icons = @[@"meat_shop_share", @"meat_shop_complaints", @"meat_shop_history"];
        NSArray *titles = @[@"分享商家", @"举报商家", @"历史评价"];
        
        @weakify(self);
        [icons enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            @strongify(self);
            UIImageView *icon = [UIImageView makeImageByImageName:obj cornerRadius:0];
            icon.userInteractionEnabled = YES;
            icon.size = CGSizeMake(22, 22);
            icon.mj_y = 18 + (28 + icon.height)*idx ;
            icon.mj_x = 14;
            [self.showIcon addSubview:icon];

            
            UILabel *label = [UILabel makeLabelWithText:titles[idx] textColor:UIColorFromRGB(0x4A4A4A) font:mFont(14) textAliment:0];
            label.size = CGSizeMake(144 - icon.right - 5, 22);
            label.mj_x = icon.right + 5;
            label.mj_y = icon.mj_y;
            [self.showIcon addSubview:label];
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(0, 50*idx, 144, 50);
            btn.tag = 200+idx;
            [self.showIcon addSubview:btn];

            [btn addTarget:self action:@selector(jumpRelatedController:) forControlEvents:UIControlEventTouchUpInside];
            
            if (idx >0) {
                CAShapeLayer *line = [CAShapeLayer layer];
                line.fillColor = UIColorFromRGB(0xEAEAEA).CGColor;
                UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 50*idx, self.showIcon.width, 1)];
                line.path = path.CGPath;
                [self.showIcon.layer addSublayer:line];
            }
            
        }];
        
        
    }
    return _showIcon;
}

- (void)jumpRelatedController:(UIButton *)btn{
    
    if (btn.tag == 200) {
        
    }else{
        
        MeatComplaintViewConroller *vc = [[MeatComplaintViewConroller alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        self.showIcon.alpha = 0;
    }
}

- (void)hideShopIcon{
    
    @weakify(self);
    [UIView animateWithDuration:AnimationTime animations:^{
        @strongify(self);
        self.showIcon.alpha = 0;
    }];

}

@end
